package com.michaelbulava.TFS.ui.wizards.common;

import com.intellij.ide.wizard.CommitStepException;
import com.intellij.openapi.module.Module;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.ui.controls.idea.SelectProjectOrModuleControl;
import com.michaelbulava.TFS.ui.framework.wizard.ExtendedWizardStep;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Created by mbulava on 7/14/2015.
 */
public class ShareProjectOrModuleWizardStep extends ExtendedWizardStep {
    public static final Object STEP_ID = new Object();

    private final IShareWizard parent;
    private final SelectProjectOrModuleControl control;

    public ShareProjectOrModuleWizardStep(IShareWizard wizard){
        super(Messages.getString("ShareProjectOrModuleWizardStep.Title"));
        this.parent = wizard;
        control = new SelectProjectOrModuleControl();

        control.setProject(wizard.getProject());

        control.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                fireStateChanged();
            }
        });

        if (wizard.getModulesToShare() != null && wizard.getModulesToShare().length > 0)
            control.setSelectedModules(wizard.getModulesToShare());

    }

    @Override
    public void _init(){
        super._init();
        control.setEntireProject(parent.getShareEntireProject());

        if (parent.getModulesToShare() != null && parent.getModulesToShare().length > 0)
            control.setSelectedModules(parent.getModulesToShare());
    }

    @Override
    public Object getStepId() {
        return STEP_ID;
    }

    @Override
    public Object getNextStepId() {
        return parent.getNextStepId(this);
    }

    @Override
    public Object getPreviousStepId() {
        return parent.getPreviousStepId(this);
    }

    @Override
    public void commit(CommitType commitType) throws CommitStepException {
        if (commitType == CommitType.Next || commitType == CommitType.Finish){
            parent.setShareEntireProject(control.isEntireProject());
            parent.setModulesToShare(control.getSelectedModules());
        }
    }

    @Override
    public boolean isComplete() {
        return control.isEntireProject() || control.getSelectedModules().length > 0;
    }

    @Override
    public JComponent getComponent() {
        return control;
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return null;
    }
}
